<?php

namespace App\Http\Controllers\Home;

use App\Models\Goods;
use App\Models\Orders;
use Illuminate\Http\Request;

//使用类
use App\Http\Controllers\Controller;


//订单控制器
class OrdersController extends Controller
{
    //生成订单
	public function index(Request $request){

	    //dd($request->all());

	    //接受收货地址ID
        $aid = $request->input("aid");

        //接收
        $ids = $request->input("ids");
        $nums = $request->input("nums");
        $prices = $request->input("prices");

        //获取用户id
        $uid = session('lenovoHomeUserInfo.id');

        //订单号生成
        $code = "DZ_".time().rand();

        //订单生成时间
        $time = time();

        //生成订单
        for($i=0;$i<count($ids);$i++){
            $data =  array();
            $data['code']=$code;
            $data['time']=$time;
            $data['uid']=$uid;
            $data['aid']=$aid;
            $data['sid']=1;//未付款
            $data['gid']=$ids[$i];
            $data['num']=$nums[$i];
            $data['price']=$prices[$i];

            //var_dump($data);
            Orders::insert($data);//添加订单
        }

        //需要删除session中对应数据

        //获取购物车中所有的商品数据
        $shop = session('shop');

        //遍历数据
        foreach($shop as $key => $value){

            foreach($ids as $k => $v){
                //判断需要删除的数据
                if($v==$value['id']){

                    unset($shop[$key]);
                }
            }


        }
        //写入session
        $request->session()->put('shop',$shop);

        return redirect("pay/$code");

	}

	//支付页面
    public function pay($code){

	    $orders = Orders::where("code",$code)->get();

	   // dd($orders);

	    return view("home.pay");
    }


	
}
